package com.livesource.client.web.sourceCode.filesTree;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Anchor;
import com.livesource.client.LiveSourceGlobalVariables;
import com.livesource.client.model.entry.ProjectClass;
import com.livesource.client.web.sourceCode.FeatureDescription;
import com.livesource.client.web.sourceCode.SourceCodePage;
import com.livesource.client.web.sourceCode.code.AceGWTEditor;
import com.livesource.client.web.sourceCode.viewOptions.OptionsIcons;
import com.livesource.client.webservices.GetFileContent;

public class AnchorFileName extends Anchor {

	public AnchorFileName(final String viewSelected, String featurePath) {

		final ProjectClass projectFeature = LiveSourceGlobalVariables.projectEntries
				.get(featurePath);

		String featureName = projectFeature.getFeatureName();

		if (FilesViewList.VIEW_SOURCE_CODE.equals(viewSelected)) {

			featureName = "<font size=1><img src='" + GWT.getModuleName()
					+ "/images/file.gif'> " + projectFeature.getFileName()
					+ "</font>";

		} else {

			featureName = "<img src='" + GWT.getModuleName()
					+ "/images/world.gif'> " + projectFeature.getFeatureName();

			if (projectFeature.getStatus() != null) {

				featureName += " <font color='red'>*</font>";
			}
		}

		this.setHTML(featureName);

		this.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {

				if (projectFeature.getFileContent() != null) {

					SourceCodePage.hpOptionsIcons.clear();
					SourceCodePage.hpOptionsIcons.add(new OptionsIcons(
							projectFeature));

					SourceCodePage.featureVisualizationPanel.clear();

					if (FilesViewList.VIEW_SOURCE_CODE.equals(viewSelected)) {

						new AceGWTEditor().showCodeEditor(projectFeature);

					} else {

						SourceCodePage.featureVisualizationPanel
								.add(new FeatureDescription(projectFeature));
					}

				} else {

					new GetFileContent(projectFeature,
							LiveSourceGlobalVariables.currentProject, null,
							null);
				}
			}
		});
	}

}
